/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pa036is.servicesimplementation;

import cz.muni.fi.pa036is.daointerface.AdmissionDAO;
import cz.muni.fi.pa036is.daointerface.PersonDAO;
import cz.muni.fi.pa036is.daointerface.SeasonDAO;
import cz.muni.fi.pa036is.dto.AdmissionFormTO;
import cz.muni.fi.pa036is.dto.AdmissionGridTO;
import cz.muni.fi.pa036is.dto.PersonDetailTO;
import cz.muni.fi.pa036is.dtoconverters.AdmissionConverter;
import cz.muni.fi.pa036is.entity.Admission;
import cz.muni.fi.pa036is.entity.Person;
import cz.muni.fi.pa036is.entity.Season;
import cz.muni.fi.pa036is.servicesinterface.AdmissionService;
import cz.muni.fi.pa036is.utilities.TimeService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Shebik
 */
@Service
public class AdmissionServiceImpl implements AdmissionService {
    
    @Autowired
    private AdmissionDAO admissionDAO;

    @Autowired
    private PersonDAO personDAO;

    @Autowired
    private SeasonDAO seasonDAO;
				private Person PersonConverter;

    @Override
    public void addAdmission(final AdmissionFormTO admissionFormTO) {
        final Admission admission = AdmissionConverter.convertToAdmission(admissionFormTO);
        admission.setDate(TimeService.getCurrentTimestamp());
        admissionDAO.addAdmission(admission);
								admissionFormTO.setId(admission.getId());
    }

    @Override
    public void editAdmission(final AdmissionFormTO admissionFormTO) {
        final Admission admission = AdmissionConverter.convertToAdmission(admissionFormTO);
        admission.setDate(TimeService.getCurrentTimestamp());
        admissionDAO.editAdmission(admission);
    }

    @Override
    public void deleteAdmission(final AdmissionGridTO admission) {
        admissionDAO.deleteAdmission(AdmissionConverter.convertToAdmission(admission));
    }

    @Override
    public AdmissionFormTO getAdmissionForm(final Long personId, final Long seasonId) {
        final Admission admission = admissionDAO.findAdmission(personId, seasonId);
        final Person person = personDAO.findPerson(admission.getPersonId());
        final Season season = seasonDAO.findSeason(admission.getSeasonId());

        return AdmissionConverter.convertToAdmissionFormTO(admission, person, season);
    }

    @Override
    public List<AdmissionGridTO> getAllAdmissions() {
        return convertToAdmissionGridTOList(admissionDAO.getAllAdmissions());
    }

    private List<AdmissionGridTO> convertToAdmissionGridTOList(
            final List<Admission> admissionList) {
        List<AdmissionGridTO> admissionGridTOList = new ArrayList<AdmissionGridTO>();

        for (final Admission admission : admissionList) {
            final Season season = seasonDAO.findSeason(admission.getSeasonId());
            final Person person = personDAO.findPerson(admission.getPersonId());
            admissionGridTOList.add(AdmissionConverter.convertToAdmissionGridTO(
                    admission, season, person));
        }

        return admissionGridTOList;
    }

				@Override
				public List<AdmissionGridTO> getAdmissionsForPerson(PersonDetailTO person) {
								List<AdmissionGridTO> admissions = new ArrayList<>();
								for (Admission a: this.admissionDAO.findAdmissionsForPerson(person.getId())) {
												Season season = this.seasonDAO.findSeason(a.getSeasonId());
												
												AdmissionGridTO admission = AdmissionConverter.convertToAdmissionGridTO(
																				a,
																				season,
																				cz.muni.fi.pa036is.dtoconverters.PersonConverter.convertToPerson(person));
												admissions.add(admission);
								}
								
								return admissions;
				}

	@Override
	public void deleteAdmission(Long personId, Long seasonId) {
		this.admissionDAO.deleteAdmission(this.admissionDAO.findAdmission(personId, seasonId));
	}

}
